package com.holfeld.teresa.vocabtrainer;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.holfeld.teresa.vocabtrainer.provider.VocablePair;

import java.util.List;

/**
 * @author Teresa Holfeld, teresa.holfeld@gmail.com
 */
public class VocableArrayAdapter extends ArrayAdapter<VocablePair> {

    private List<VocablePair> mVocables;
    private Context mContext;

    static class ViewHolder {
        public TextView vocable1TextView;
        public TextView vocable2TextView;
    }

    public VocableArrayAdapter(Context context, List<VocablePair> objects) {
        super(context, R.layout.vocable_list_item, objects);
        mContext = context;
        mVocables = objects;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View rowView = convertView;
        if (rowView == null) {
            LayoutInflater inflater = (LayoutInflater) mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            rowView = inflater.inflate(R.layout.vocable_list_item, null, true);
            ViewHolder viewHolder = new ViewHolder();
            viewHolder.vocable1TextView = (TextView) rowView.findViewById(R.id.vocable1);
            viewHolder.vocable2TextView = (TextView) rowView.findViewById(R.id.vocable2);
            rowView.setTag(viewHolder);
        }

        ViewHolder holder = (ViewHolder) rowView.getTag();
        VocablePair vocablePair = mVocables.get(position);
        String vocable1 = vocablePair.getVocable1();
        String vocable2 = vocablePair.getVocable2();
        holder.vocable1TextView.setText(vocable1);
        holder.vocable2TextView.setText(vocable2);
        return rowView;
    }
}
